package com.summer.common.jsonTransfer;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.summer.common.OperateThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("正在调用通用字段自动填充方法：添加操作...");
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        long id = OperateThreadLocal.getLocalThreadAttribute();
        metaObject.setValue("createUser", id);
        metaObject.setValue("updateUser", id);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("正在调用通用字段自动填充方法：更新操作...");
        metaObject.setValue("updateTime", LocalDateTime.now());
        long id = OperateThreadLocal.getLocalThreadAttribute();
        metaObject.setValue("updateUser", id);
    }
}
